package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.ClassCollectBean;
import cst.neu.yuejian_31.iface.ClassCollectIface;
import cst.neu.yuejian_31.iface.ClassCollectListener;
import cst.neu.yuejian_31.service.ClassCollectService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/25.
 */

public class ClassCollectModel implements ClassCollectIface {
    private Retrofit retrofit;
    public ClassCollectModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void classCollect(String userId, String classId, final ClassCollectListener classCollectListener) {
        ClassCollectService classCollectService=retrofit.create(ClassCollectService.class);
        Call<ClassCollectBean>call=
                classCollectService.classCollect(userId,classId);
        call.enqueue(new Callback<ClassCollectBean>() {
            @Override
            public void onResponse(Call<ClassCollectBean> call, Response<ClassCollectBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    classCollectListener.onSuccess(response.body());
                }else {
                    classCollectListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<ClassCollectBean> call, Throwable t) {
                classCollectListener.onFail(t.toString());
            }
        });
    }
}
